/**
 * Tabbar 高度计算工具
 * 用于解决小程序端安卓和苹果高度不一致的问题
 */
import { computed, ref } from 'vue'

export interface TabbarHeightInfo {
  baseHeight: number
  actualHeight: number
  safeAreaBottom: number
  platform: string
  isIOS: boolean
  isAndroid: boolean
  hasNotch: boolean
}

/**
 * 获取 Tabbar 高度信息的 composable
 */
export function useTabbarHeight() {
  const tabbarInfo = ref<TabbarHeightInfo>({
    baseHeight: 168,
    actualHeight: 168,
    safeAreaBottom: 0,
    platform: 'unknown',
    isIOS: false,
    isAndroid: false,
    hasNotch: false,
  })

  /**
   * 初始化 tabbar 高度信息
   */
  function initTabbarHeight() {
    try {
      const systemInfo = uni.getSystemInfoSync()
      const windowInfo = uni.getWindowInfo()

      // 计算安全区域
      const safeAreaBottom = windowInfo.safeArea
        ? windowInfo.windowHeight - windowInfo.safeArea.bottom
        : 0

      // 判断平台
      const isIOS = systemInfo.platform === 'ios'
      const isAndroid = systemInfo.platform === 'android'
      const hasNotch = safeAreaBottom > 0

      // 根据平台计算高度
      let baseHeight = 168
      let actualHeight = 168

      // #ifdef MP-WEIXIN
      if (isIOS) {
        // iOS 小程序根据是否有刘海屏调整
        baseHeight = hasNotch ? 180 : 168
        actualHeight = baseHeight
      }
      else if (isAndroid) {
        // Android 小程序高度调整
        baseHeight = 160
        actualHeight = baseHeight
      }
      // #endif

      // #ifdef APP-PLUS
      if (isIOS) {
        baseHeight = hasNotch ? 190 : 170
        actualHeight = baseHeight
      }
      else if (isAndroid) {
        baseHeight = 165
        actualHeight = baseHeight
      }
      // #endif

      // #ifdef H5
      baseHeight = 150
      actualHeight = baseHeight
      // #endif

      tabbarInfo.value = {
        baseHeight,
        actualHeight,
        safeAreaBottom,
        platform: systemInfo.platform,
        isIOS,
        isAndroid,
        hasNotch,
      }

      console.log('Tabbar高度信息初始化:', tabbarInfo.value)
    }
    catch (error) {
      console.error('初始化tabbar高度信息失败:', error)
    }
  }

  /**
   * 获取 tabbar 占位高度（包含安全区域）
   */
  const placeholderHeight = computed(() => {
    const info = tabbarInfo.value
    let height = info.actualHeight

    // 添加平台特定的额外间距
    if (info.isIOS) {
      height += 10 // iOS 额外间距
    }
    else if (info.isAndroid) {
      height += 5 // Android 额外间距
    }

    return height
  })

  /**
   * 获取页面内容的底部 padding（避免被 tabbar 遮挡）
   */
  const contentBottomPadding = computed(() => {
    return `${placeholderHeight.value}rpx`
  })

  /**
   * 获取页面可用高度（减去 tabbar 高度）
   */
  const availableHeight = computed(() => {
    const info = tabbarInfo.value
    // 100vh 减去 tabbar 高度和安全区域
    return `calc(100vh - ${info.actualHeight}rpx - ${info.safeAreaBottom}px)`
  })

  return {
    tabbarInfo,
    placeholderHeight,
    contentBottomPadding,
    availableHeight,
    initTabbarHeight,
  }
}

/**
 * 获取当前平台的 tabbar 样式类名
 */
export function getTabbarPlatformClass() {
  try {
    const systemInfo = uni.getSystemInfoSync()
    const isIOS = systemInfo.platform === 'ios'
    const isAndroid = systemInfo.platform === 'android'

    return {
      'ios-platform': isIOS,
      'android-platform': isAndroid,
      'mp-platform': true, // 小程序平台标识
    }
  }
  catch (error) {
    console.error('获取平台类名失败:', error)
    return {}
  }
}
